package com.markyao.service.db;


import com.markyao.recos.conf.RecosConfService;
import com.markyao.utils.DateParseUtils;
import com.markyao.vo.ConfigInfoVo;
import com.markyao.vo.pojo.ConfigInfo;
import com.markyao.vo.pojo.HisConfigInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class AbstractConfigService {

    @Autowired
    RecosConfService recosConfService;
    @Autowired
    @Qualifier("appsDataId")
    Map<String,Set<String>> appsDataId;

    protected boolean adviceRefresh(ConfigInfoVo configInfov) {
        if (StringUtils.hasText(configInfov.getAppName())){
            recosConfService.refreshConf(configInfov.getAppName(),configInfov);
            return true;
        }else {
            Set<String> set = appsDataId.get(configInfov.getDataId());
            for (String appName : set) {
                boolean b = recosConfService.refreshConf(appName,configInfov);
                if (!b){
                    return false;
                }
            }
        }
        return true;
    }

    protected ConfigInfoVo abscopy(ConfigInfo configInfo){
        ConfigInfoVo configInfoVo = new ConfigInfoVo();
        BeanUtils.copyProperties(configInfo,configInfoVo);
        if (configInfo.getId()!=null){
            configInfoVo.setId(configInfo.getId().toString());
        }
        return configInfoVo;
    }
    protected ConfigInfo abscopy(ConfigInfoVo configInfovo){
        ConfigInfo configInfo=new ConfigInfo();
        BeanUtils.copyProperties(configInfovo,configInfo);
        if (configInfovo.getId()!=null && configInfovo.getId().length()>0){
            configInfo.setId(Long.parseLong(configInfovo.getId()));
        }
        return configInfo;
    }

    protected List<ConfigInfoVo> abscopyList(List<ConfigInfo>configInfoList){
        List<ConfigInfoVo>configInfoVos=new ArrayList<>(configInfoList.size());
        for (ConfigInfo configInfo : configInfoList) {
            ConfigInfoVo copy = abscopy(configInfo);
            configInfoVos.add(copy);
        }
        return configInfoVos;
    }

    protected HisConfigInfo copy0(ConfigInfo configInfo){
        HisConfigInfo hisConfigInfo=new HisConfigInfo();
        BeanUtils.copyProperties(configInfo,hisConfigInfo);
        hisConfigInfo.setGmtModified(new Date());
        hisConfigInfo.setOpType(configInfo.getType());
        return hisConfigInfo;
    }

    protected ConfigInfo copy0(HisConfigInfo hisConfigInfo){
        ConfigInfo configInfo=new ConfigInfo();
        BeanUtils.copyProperties(hisConfigInfo,configInfo);
        configInfo.setGmtModified(new Date());
        configInfo.setType(hisConfigInfo.getOpType());
        return configInfo;
    }

    protected ConfigInfoVo copy(HisConfigInfo configInfo){
        ConfigInfoVo configInfoVo = new ConfigInfoVo();
        BeanUtils.copyProperties(configInfo,configInfoVo);
        if (configInfo.getId()!=null){
            configInfoVo.setId(configInfo.getId().toString());
        }
        return configInfoVo;
    }
    protected ConfigInfo copy(ConfigInfoVo configInfovo){
        ConfigInfo configInfo=new ConfigInfo();
        BeanUtils.copyProperties(configInfovo,configInfo);
        if (configInfovo.getId()!=null && configInfovo.getId().length()>0){
            configInfo.setId(Long.parseLong(configInfovo.getId()));
        }
        return configInfo;
    }

    protected List<ConfigInfoVo> copyList(List<HisConfigInfo>configInfoList){
        List<ConfigInfoVo>configInfoVos=new ArrayList<>(configInfoList.size());
        for (HisConfigInfo configInfo : configInfoList) {
            ConfigInfoVo copy = copy(configInfo);
            configInfoVos.add(copy);
            copy.setGmtCreate(DateParseUtils.dateParse(configInfo.getGmtCreate()));
        }
        return configInfoVos;
    }
}
